
import {
  queryPromotionOrderList
} from "../../../../service/jb_promotion_service"
import {
  queryOaActivityList,
  delActivityReq,
  showOrHideActivity,
} from "../../../../service/jb_activity_service"
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    menuTop: app.globalData.menuButton.top,
    menuHeight: app.globalData.menuButton.height,
    topSwiperIndex: 1,
    requestData: {
      page: 1,
      limit: 20,
    },
    page: 1,
    limit: 20,
    status: 1,
    dataList: [

    ],
    totalCount: 0,
    isShowLoading: false,//加载状态出现
    isBottomShow: false,//是否没有更多数据了
    refreshFlag: false,

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {


  },
  topSwiperChange(e) {
    let index = e.currentTarget.dataset.index;
    this.setData({
      topSwiperIndex: index,
      status: index,
      page: 1,
      isBottomShow: false,
    })
    this.queryPromotionOrderList('refresh');
  },
  //跳转到详情
  pushToDetail(e) {
    let item = e.currentTarget.dataset.item;
    app.globalData.activityDetail = item;
    wx.navigateTo({
      url: '/featurePackage/pages/oaManager/createActivity/createActivity',
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.queryPromotionOrderList('refresh');
  },
  createActivityAction() {
    wx.navigateTo({
      url: '/featurePackage/pages/oaManager/createActivity/createActivity',
    })
  },
  queryPromotionOrderList(type) {
    wx.showLoading();
    queryOaActivityList(this.data.status, this.data.page, this.data.limit).then(res => {
      wx.hideLoading();
      if (res.code == 200 && res.data) {
        let arr = (type == 'refresh' ? [] : this.data.dataList || []);
        arr = arr.concat(res.data.list || [])
        this.setData({
          dataList: arr,
          refreshFlag: false,
          isShowLoading: false,
          isBottomShow: res.data.totalPage == this.data.page ? true : false,
        })
      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err || '加载失败',
        icon: "none"
      })
      if (this.data.page != 1) {
        this.setData({
          page: this.data.page - 1
        })
      }
      this.setData({
        refreshFlag: false,
      })
      console.error('JJLog:queryPromotionOrderList', err);
    })
  },

  //下拉刷新
  handleRefresh() {
    // TODO
    this.setData({
      page: 1,
      isBottomShow: false,
      refreshFlag: true,
    })
    this.queryPromotionOrderList('refresh');
  },
  handleLoadmore() {
    console.log('加载更多');
    if (this.data.isBottomShow) {
      return;
    }
    this.setData({
      isShowLoading: true,
      page: this.data.page + 1
    })
    this.queryPromotionOrderList()
  },

  //删除一个活动
  deleteActivityAction(e) {
    let id = e.currentTarget.dataset.id;
    wx.showModal({
      title: '提示',
      content: '确认删除当前活动?',
      confirmColor: "#ff0000",
      success: res => {
        if (res.confirm) {
          this.deleteCurrentActivity(id);
        }
      }
    })
  },
  deleteCurrentActivity(id) {
    wx.showLoading()
    delActivityReq({ id: id }).then(res => {
      wx.hideLoading()
      if (res.code == 200) {
        wx.showToast
          ({
            title: '删除成功...',
            icon: "success"
          })

      }
    }).catch(err => {
      console.error("JJLog:delActivityReq", err);
      wx.hideLoading()
      wx.showToast
        ({
          title: err || '删除失败...',
          icon: "none"
        })
    })
  },
  
  //上架或者下架操作:
  upOrDownClick(e) {
    let item = e.currentTarget.dataset.item;
    let index = e.currentTarget.dataset.index;
    let isShow = item.isShow;
    let remindText = "是否下架当前活动";
    if (!isShow) {
      remindText = "是否上架当前活动"
    }
    wx.showModal({
      title: '提示',
      content: remindText,
      confirmColor: "#ff0000",
      success: res => {
        if (res.confirm) {
          this.onOrOffHouseRequest(item.id, index, isShow)
        }
      }
    })


  },
  //上架下架请求:
  onOrOffHouseRequest(id, index, isShow) {
    wx.showLoading();
    showOrHideActivity({id:id,isShow:!isShow}).then(res => {
      wx.hideLoading();
      if (res.code == 200) {
        let successText = "下架成功";
        if (!isShow) {
          successText = "上架成功"
        }
        wx.showToast({
          title: successText,
          icon: "success"
        })
        let item = 'dataList[' + index + '].isShow';
        this.setData({
          [item]: !isShow,
        })
      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err || "操作失败",
        icon: "none"
      })
      console.error("JJLog:onOrOffHouse", err);
    })
  },
})